package com.gugu.procedure.service;

import com.gugu.procedure.bean.Staff;
import com.gugu.procedure.common.Constant;
import com.gugu.procedure.common.MessageResult;
import com.gugu.procedure.dao.StaffDao;
import com.gugu.procedure.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class StaffService {

    @Autowired
    private StaffDao staffDao;

    public MessageResult add(Staff staff) {
        Staff bean = staffDao.findTopByNumber(staff.getNumber());
        if (Objects.nonNull(bean)) {
            staff.setId(bean.getId());
        }
        staff.setCreateTime(DateUtil.getFormatedDateTime());
        staff.setIsDel(Constant.IS_DEL_NO);
        staffDao.save(staff);
        return MessageResult.success();
    }

    public MessageResult saveAll(List<Staff> list) {
        for (Staff staff : list) {
            this.add(staff);
        }
        return MessageResult.success();
    }

    public MessageResult update(Staff staff) {
        Staff bean = staffDao.findTopByNumber(staff.getNumber());
        if (Objects.nonNull(bean)) {
            staff.setId(bean.getId());
        }
        staffDao.save(staff);
        return MessageResult.success();
    }


    public Staff findByStaffNumber(String number) {
        return staffDao.findTopByNumber(number);
    }

    public MessageResult delete(String number) {
        Staff bean = staffDao.findTopByNumber(number);
        bean.setIsDel(Constant.IS_DEL_YES);
        staffDao.save(bean);
        return MessageResult.success();
    }

    public MessageResult page(String name, String mobile, String number, int pageNo, int pageSize) {
        Specification<Staff> spec = (Specification<Staff>) (root, query, cb) -> {
            List<Predicate> list = new ArrayList<>();
            if (StringUtils.hasText(name)) {
                list.add(cb.equal(root.get("name").as(String.class), name));
            }
            if (StringUtils.hasText(mobile)) {
                list.add(cb.equal(root.get("mobile").as(String.class), mobile));
            }
            if (StringUtils.hasText(number)) {
                list.add(cb.equal(root.get("number").as(String.class), number));
            }
            list.add(cb.equal(root.get("isDel").as(String.class), Constant.IS_DEL_NO));
            Predicate[] pre = new Predicate[list.size()];
            query.where(list.toArray(pre));
            return query.getRestriction();
        };
        PageRequest pr = PageRequest.of(pageNo - 1, pageSize, Sort.Direction.DESC, "createTime");
        Page<Staff> recordsPage = staffDao.findAll(spec, pr);
        MessageResult mr = MessageResult.success();
        mr.setTotal(recordsPage.getTotalElements());
        mr.setTotalElement(recordsPage.getContent());
        return mr;
    }


}
